package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DataItems;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDataItemsService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("dataItems")
public class DataItemsController {
    @Autowired
    private IDataItemsService dataItemsService;

    @RequiresPermissions("dataItems:index")
    @PermissionName("字典明细首页")
    @RequestMapping("")
    public String index() {
        return "dataItems/list";
    }

    @RequiresPermissions("dataItems:list")
    @PermissionName("字典明细数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) {
        List<DataItems> list = dataItemsService.list();
        PageResult p = new PageResult();
        p.setRows(list);
        p.setTotal(list.size());
        return p;

    }

    @RequiresPermissions("dataItems:save")
    @PermissionName("菜单保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(DataItems entity) {
        JsonResult result = new JsonResult();
        try {
            dataItemsService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("dataItems:update")
    @PermissionName("数据明细更新")
    public JsonResult update(DataItems entity) {
        JsonResult result = new JsonResult();
        try {
            dataItemsService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("更新操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("dataItems:delete")
    @PermissionName("数据明细删除")
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            dataItemsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("删除操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("querydataItems")
    @ResponseBody
    //查询所有字典明细数据
    public List<DataItems> querydataItems() {
        return dataItemsService.list();
    }

    @RequestMapping("queryItemsBySn")
    @ResponseBody
    //根据数据字典的sn查询字典明细数据
    public List<DataItems> queryItemsBySn(String sn) {
        return dataItemsService.queryItemsBySn(sn);
    }

    @RequestMapping("queryItemsByName")
    @ResponseBody
    //根据数据字典的sn查询字典明细数据
    public String queryItemsByName(String name, HttpServletResponse response) {
        System.out.println(name);
        try {
            name = URLDecoder.decode(name, "UTF-8");
            System.out.println(name);
            String byName = dataItemsService.queryItemsByName(name);
           /* response.setContentType("text/xml;charset=UTF-8"); //这行放在流输出前才好使
            PrintWriter out = response.getWriter();
            out.println(byName);*/
            return byName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
